package com.samp.lang.classloader;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class EncryptServiceImpl implements EncryptService {
    private static String algorithm = "DES";

    public String encrypt(String msg, String key)throws Exception{
        Cipher cipher = Cipher.getInstance(algorithm);
        SecretKey secretKey = new SecretKeySpec(parseHexStr2Byte(key), algorithm);
        cipher.init(Cipher.ENCRYPT_MODE,secretKey );
        byte[] result = cipher.doFinal(msg.getBytes());
        return parseByte2HexStr(result);
    }

    public String decrypt(String encryptMsg,String key)throws Exception{
        Cipher cipher = Cipher.getInstance(algorithm);
        SecretKey secretKey = new SecretKeySpec(parseHexStr2Byte(key), algorithm);
        cipher.init(Cipher.DECRYPT_MODE,secretKey );
        byte[] result =  cipher.doFinal(parseHexStr2Byte(encryptMsg));
        return new String(result);
    }

    public String getKey()throws Exception{
        KeyGenerator keyGenerator = KeyGenerator.getInstance(algorithm);
        keyGenerator.init(56);
        SecretKey secretKey = keyGenerator.generateKey();
        return parseByte2HexStr(secretKey.getEncoded());
    }

    /**将二进制转换成16进制
     * @param buf
     * @return
     */
    public String parseByte2HexStr(byte buf[]) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < buf.length; i++) {
            String hex = Integer.toHexString(buf[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }

    public byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1)
            return null;
        byte[] result = new byte[hexStr.length()/2];
        for (int i = 0;i< hexStr.length()/2; i++) {
            int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);
            int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }

    public void setAlgorithm(String algorithm) {
        EncryptServiceImpl.algorithm = algorithm;
    }

    public String getAlgorithm() {
        return algorithm;
    }

    public void test() {
        try {
            EncryptServiceImpl encryptServiceImpl = new EncryptServiceImpl();
            String key = encryptServiceImpl.getKey();
            System.out.println("key="+key);
            String msg = "这是一个测试程序";
            System.out.println("原文："+msg);
            String encryptMsg = encryptServiceImpl.encrypt(msg,key);
            System.out.println("加密后："+encryptMsg);
            String decryptMsg = encryptServiceImpl.decrypt(encryptMsg,key);
            System.out.println("解密后："+decryptMsg);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        EncryptService encryptService = new EncryptServiceImpl();
        encryptService.test();
    }
}
